package com.example.supermarket.service.impl;

import com.example.supermarket.model.Fruit;
import com.example.supermarket.model.ShoppingCart;
import com.example.supermarket.service.PriceCalculatorService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 价格计算服务实现类
 */
@Service
public class PriceCalculatorServiceImpl implements PriceCalculatorService {
    private static final BigDecimal STRAWBERRY_DISCOUNT = new BigDecimal("0.8"); // 草莓折扣
    private static final BigDecimal FULL_REDUCTION_THRESHOLD = new BigDecimal("100.0"); // 满减阈值
    private static final BigDecimal FULL_REDUCTION_AMOUNT = new BigDecimal("10.0"); // 满减金额

    @Override
    public BigDecimal calculatePriceForCustomerA(int appleQuantity, int strawberryQuantity) {
        ShoppingCart cart = new ShoppingCart();
        cart.addItem(Fruit.APPLE, appleQuantity);
        cart.addItem(Fruit.STRAWBERRY, strawberryQuantity);
        return calculateTotalPrice(cart, false, false);
    }

    @Override
    public BigDecimal calculatePriceForCustomerB(int appleQuantity, int strawberryQuantity, int mangoQuantity) {
        ShoppingCart cart = new ShoppingCart();
        cart.addItem(Fruit.APPLE, appleQuantity);
        cart.addItem(Fruit.STRAWBERRY, strawberryQuantity);
        cart.addItem(Fruit.MANGO, mangoQuantity);
        return calculateTotalPrice(cart, false, false);
    }

    @Override
    public BigDecimal calculatePriceForCustomerC(int appleQuantity, int strawberryQuantity, int mangoQuantity) {
        ShoppingCart cart = new ShoppingCart();
        cart.addItem(Fruit.APPLE, appleQuantity);
        cart.addItem(Fruit.STRAWBERRY, strawberryQuantity);
        cart.addItem(Fruit.MANGO, mangoQuantity);
        return calculateTotalPrice(cart, true, false);
    }

    @Override
    public BigDecimal calculatePriceForCustomerD(int appleQuantity, int strawberryQuantity, int mangoQuantity) {
        ShoppingCart cart = new ShoppingCart();
        cart.addItem(Fruit.APPLE, appleQuantity);
        cart.addItem(Fruit.STRAWBERRY, strawberryQuantity);
        cart.addItem(Fruit.MANGO, mangoQuantity);
        return calculateTotalPrice(cart, true, true);
    }

    @Override
    public BigDecimal calculateTotalPrice(ShoppingCart cart, boolean isStrawberryOnSale, boolean isFullReductionApplied) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        
        for (var entry : cart.getItems().entrySet()) {
            Fruit fruit = entry.getKey();
            int quantity = entry.getValue();
            
            BigDecimal pricePerJian = fruit.getPricePerJian();
            
            // 应用草莓折扣
            if (isStrawberryOnSale && fruit == Fruit.STRAWBERRY) {
                pricePerJian = pricePerJian.multiply(STRAWBERRY_DISCOUNT)
                        .setScale(2, RoundingMode.HALF_UP);
            }
            
            // 计算该水果的总价并累加到总价格中
            BigDecimal itemTotal = pricePerJian.multiply(BigDecimal.valueOf(quantity))
                    .setScale(2, RoundingMode.HALF_UP);
            totalPrice = totalPrice.add(itemTotal);
        }
        
        // 应用满减
        if (isFullReductionApplied && totalPrice.compareTo(FULL_REDUCTION_THRESHOLD) >= 0) {
            totalPrice = totalPrice.subtract(FULL_REDUCTION_AMOUNT)
                    .setScale(2, RoundingMode.HALF_UP);
        }
        
        return totalPrice;
    }
}